﻿/*
Dang ky AngularJS application ngRoute
*/
var app = angular.module('SmatimeApplication', ['ngRoute', 'ui.bootstrap', 'validator']);

//Dang ky app service de chia se du lieu giua cac controller voi nhau
app.factory('service', function () {
    return new SmatimeService();
});

app.directive("ngMatchWithField", function () {
    return {
        require: "ngModel",
        link: function (scope, element, attrs, ctrl) {
            element.bind('keyup', function () {
                var firstPasswordField = '#' + attrs.ngMatchWithField;
                scope.$apply(function () {
                    var isValid = element.val() === $(firstPasswordField).val();
                    ctrl.$setValidity('ngMatchWithField', isValid);
                });
            });
        }
    };
});
app.directive('dateTime', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datetimepicker({
                format: "MM-yyyy",
                viewMode: "months",
                minViewMode: "months",
                pickTime: false,
            }).on('changeDate', function (e) {
                ngModelCtrl.$setViewValue(e.date);
                scope.$apply();
            });
        }
    };
});

//Dang ky cac view templates
app.config(
	['$routeProvider','$validatorProvider',
		function ($routeProvider, $validatorProvider) {

            //// If path does not exist, redirect to Login
		    //$routeProvider.otherwise({
		    //    redirectTo: Views[0].path
		    //});

		    $routeProvider
		        .when("/StaffTimeTest", {
		            templateUrl: "Views/templates/StaffTimeTest.html",
		        })
		        .when("/StaffTimeTestEdit/", {
		            templateUrl: "Views/templates/StaffTimeTestEdit.html",
		        })
		        .when("/StaffTimeTestEdit/:Id", {
		            templateUrl: "Views/templates/StaffTimeTestEdit.html",
		        })
		        .when("/AccessHistory", {
		            templateUrl: "Views/templates/AccessHistory.html",
		        })
		        .when("/AttendanceList", {
		            templateUrl: "Views/templates/AttendanceList.html",
		        })
		        .when("/AttendanceList/:Id", {
		            templateUrl: "Views/templates/AttendanceList.html",
		        })
		        .when("/AttendanceStatus", {
		            templateUrl: "Views/templates/AttendanceStatus.html",
		        })
		        .when("/CompanyEdit", {
		            templateUrl: "Views/templates/CompanyEdit.html",
		        })
		        .when("/CsvExport", {
		            templateUrl: "Views/templates/CsvExport.html",
		        })
		        .when("/AccessHistoryContent", {
		            templateUrl: "Views/templates/AccessHistoryContent.html",
		        })
		        .when("/StaffTime/", {
		            templateUrl: "Views/templates/StaffTime.html",
		        })
		        .when("/StaffTimeEdit/", {
		            templateUrl: "Views/templates/StaffTimeEdit.html",
		        })
		        .when("/StaffTimeEdit/:Id", {
		            templateUrl: "Views/templates/StaffTimeEdit.html",
		        })
		        .when("/StaffTimeEditConfirm", {
		            templateUrl: "Views/templates/StaffTimeEditConfirm.html",
		        })
		        .when("/StaffTimeEditConfirm/:Id", {
		            templateUrl: "Views/templates/StaffTimeEditConfirm.html",
		        })
		        .when("/Top", {
		            templateUrl: "Views/templates/Top.html",
		        })
		        .when("/UserMaster", {
		            templateUrl: "Views/templates/UserMaster.html",
		        })
		        .when("/ForgotPassword", {
		            templateUrl: "Views/templates/ForgotPassword.html",
		        })
		        .when("/Register", {
		            templateUrl: "Views/templates/Register.html",
		        })
		        //==================== coder :hong   
		        //Staff
		        .when("/StaffEdit", {
		            templateUrl: "Views/templates/StaffEdit.html",
		        })
		        .when("/StaffEdit/:Id", {
		            templateUrl: "Views/templates/StaffEdit.html",
		        })
		        .when("/StaffList", {
		            templateUrl: "Views/templates/StaffList.html",
		        })
		        //Store
		        .when("/StoreEdit", {
		            templateUrl: "Views/templates/StoreEdit.html",
		        })
		        .when("/StoreEdit/:Id", {
		            templateUrl: "Views/templates/StoreEdit.html",
		        })
		        .when("/StoreList", {
		            templateUrl: "Views/templates/StoreList.html",
		        })
		        //Authentication
		        .when("/Login", {
		            templateUrl: "Views/templates/Login.html",
		        })
		        .when("/RegisterConfirm", {
		            templateUrl: "Views/templates/RegisterConfirm.html",
		        })
		        .when("/RegisterSuccess", {
		            templateUrl: "Views/templates/RegisterSuccess.html",
		        })
		        .when("/ResetPassword", {
		            templateUrl: "Views/templates/ResetPassword.html",
		        })
		        .when("/ForgotPassword", {
		            templateUrl: "Views/templates/ForgotPassword.html",
		        })
		        .when("/Message", {
		            templateUrl: "Views/templates/Message.html",
		        })
		        .when("/RegisterCompleteInformation", {
		            templateUrl: "Views/templates/RegisterCompleteInformation.html",
		        });

            //===================== end hong

            //.otherwise({
            //    redirectTo: "/StaffTimeTest"
            //});

		    // use the HTML5 History API
		    //$locationProvider.html5Mode(true);


		    //var $errorTemplate = angular.element("<label class='label label-danger'></label>");


		    $validatorProvider.register("required", {
		        invoke: "watch",
		        validator: /.+/,
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "This field is required.";
		            //angular.element(element).after($errorTemplate.html(message))

		            if (element.val() && attrs.validatorRequiredMessage) {
		                message = attrs.validatorRequiredMessage;
		            }
		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });


		    //# blur - required
		    $validatorProvider.register("requiredBlur", {
		        invoke: "blur",
		        validator: /.+/,
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "This field is required.";
		            //angular.element(element).after($errorTemplate.html(message))

		            if (element.val() && attrs.validatorRequiredMessage) {
		                message = attrs.validatorRequiredMessage;
		            }
		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });

		    $validatorProvider.register("matchField", {
		        invoke: "blur",
		        validator: function (value, scope, element, attrs, $injector) {
		            return angular.element("#" + attrs.validatorMatchWith).val() === value
		        },
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "Confirm password and new password doesn't match.";

		            if (element.val() && attrs.validatorMatchFieldMessage) {
		                message = attrs.validatorMatchFieldMessage;
		            }
		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });

		    $validatorProvider.register("notHaveSpecialChars", {
		        invoke: "watch",
		        validator: /^((?!([*'[\]\^%?#＾])).)*$/,
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "Special characters is not allowed.";

		            if (element.val() && attrs.validatorNotHaveSpecialCharsMessage) {
		                message = attrs.validatorNotHaveSpecialCharsMessage;
		            }

		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });

		    $validatorProvider.register("dateBefore", {
		        invoke: "watch",
		        validator: function (value, scope, element, attrs, $injector) {
		            if (element.val() && attrs.validatorDateBefore.length > 0) {
		                return new Date(value).compareTo(new Date(attrs.validatorDateBefore).clearTime()) <= 0
		            } else {
		                return true
		            }
		        },
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "From Date must be before To Date";

		            if (element.val() && attrs.validatorDateBeforeMessage) {
		                message = attrs.validatorDateBeforeMessage;
		            }

		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });

		    $validatorProvider.register("dateAfter", {
		        invoke: "watch",
		        validator: function (value, scope, element, attrs, $injector) {
		            if (element.val() && attrs.validatorDateAfter.length > 0) {
		                return new Date(value).compareTo(new Date(attrs.validatorDateAfter).clearTime()) > -1
		            } else {
		                return true
		            }
		        },
		        error: function (value, scope, element, attrs, $injector) {
		            var message = "To Date must be after From Date";

		            if (element.val() && attrs.validatorDateAfterMessage) {
		                message = attrs.validatorDateAfterMessage;
		            }

		            $validatorProvider.convertError(message)(value, scope, element, attrs)
		        }
		    });

		}
]);

